package androidx.test.espresso.action;

import android.view.MotionEvent;
import androidx.test.espresso.UiController;
import androidx.test.espresso.action.Swiper;
import androidx.test.espresso.core.internal.deps.guava.base.Preconditions;
import java.lang.reflect.Array;
import java.util.ArrayList;

/* loaded from: classes.dex */
public enum Swipe implements Swiper {
    FAST { // from class: androidx.test.espresso.action.Swipe.1
        @Override // androidx.test.espresso.action.Swiper
        public Swiper.Status sendSwipe(UiController uiController, float[] startCoordinates, float[] endCoordinates, float[] precision) {
            return Swipe.sendLinearSwipe(uiController, startCoordinates, endCoordinates, precision, 150);
        }
    },
    SLOW { // from class: androidx.test.espresso.action.Swipe.2
        @Override // androidx.test.espresso.action.Swiper
        public Swiper.Status sendSwipe(UiController uiController, float[] startCoordinates, float[] endCoordinates, float[] precision) {
            return Swipe.sendLinearSwipe(uiController, startCoordinates, endCoordinates, precision, Swipe.SWIPE_SLOW_DURATION_MS);
        }
    };
    
    private static final int SWIPE_EVENT_COUNT = 10;
    private static final int SWIPE_FAST_DURATION_MS = 150;
    private static final int SWIPE_SLOW_DURATION_MS = 1500;
    private static final String TAG = Swipe.class.getSimpleName();

    private static float[][] interpolate(float[] start, float[] end, int steps) {
        Preconditions.checkElementIndex(1, start.length);
        Preconditions.checkElementIndex(1, end.length);
        float[][] fArr = (float[][]) Array.newInstance(float.class, steps, 2);
        for (int i = 1; i < steps + 1; i++) {
            int i2 = i - 1;
            float f = i;
            float f2 = steps + 2.0f;
            fArr[i2][0] = start[0] + (((end[0] - start[0]) * f) / f2);
            fArr[i2][1] = start[1] + (((end[1] - start[1]) * f) / f2);
        }
        return fArr;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static Swiper.Status sendLinearSwipe(UiController uiController, float[] startCoordinates, float[] endCoordinates, float[] precision, int duration) {
        Preconditions.checkNotNull(uiController);
        Preconditions.checkNotNull(startCoordinates);
        Preconditions.checkNotNull(endCoordinates);
        Preconditions.checkNotNull(precision);
        float[][] interpolate = interpolate(startCoordinates, endCoordinates, 10);
        ArrayList<MotionEvent> arrayList = new ArrayList();
        MotionEvent obtainDownEvent = MotionEvents.obtainDownEvent(startCoordinates, precision);
        arrayList.add(obtainDownEvent);
        try {
            try {
                long length = duration / interpolate.length;
                long downTime = obtainDownEvent.getDownTime();
                for (float[] fArr : interpolate) {
                    downTime += length;
                    arrayList.add(MotionEvents.obtainMovement(obtainDownEvent.getDownTime(), downTime, fArr));
                }
                arrayList.add(MotionEvent.obtain(obtainDownEvent.getDownTime(), length + downTime, 1, endCoordinates[0], endCoordinates[1], 0));
                uiController.injectMotionEventSequence(arrayList);
                for (MotionEvent motionEvent : arrayList) {
                    motionEvent.recycle();
                }
                return Swiper.Status.SUCCESS;
            } catch (Exception unused) {
                Swiper.Status status = Swiper.Status.FAILURE;
                for (MotionEvent motionEvent2 : arrayList) {
                    motionEvent2.recycle();
                }
                return status;
            }
        } catch (Throwable th) {
            for (MotionEvent motionEvent3 : arrayList) {
                motionEvent3.recycle();
            }
            throw th;
        }
    }
}
